import requests
from util.logger import Logger

class BaseRequest(object):
    def sendrequest(self,method,url,params=None,data=None,header=None,cookie=None,):
        if method == 'get':
            try:
                rt = requests.get(url=url,params=params,headers=header,cookies=cookie)
                Logger().logger.info('发送get请求成功{}')
                return rt
            except Exception as e:
                # raise Exception('发送get请求失败，请求结果为{}'.format(e))
                Logger().logger.error('发送get请求失败，请求结果为{}'.format(e))
                raise Exception('发送get请求失败，请求结果为{}'.format(e))
        elif method == 'post':
            try:
                rt = requests.post(url=url,data=data,headers=header,cookies=cookie)
                return rt
            except Exception as e:
                Logger().logger.error('发送psot请求失败，请求结果为{}'.format(e))
                raise Exception('发送post请求失败，请求结果为{}'.format(e))

if __name__ == '__main__':

    baseRequest = BaseRequest()
    a = baseRequest.sendrequest(method='post',url='https://v1.hitokot.cn/')
    print(a.json())
    print(a.json()['hitokoto'])